package com.dd.controoler;

import com.dd.mapper.AddressMapper;
import com.dd.mapper.OrderMaper;
import com.dd.mapper.RoleMapper;
import com.dd.mapper.UserOrderMapper;
import com.dd.pojo.Order;
import com.dd.pojo.User;
import com.dd.serivce.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserControoler {

    @Autowired
    private UserSerivce userSerivcel;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private OrderMaper orderMaper;

    /**
     * 添加
     *
     * @param user
     * @return
     */
    @RequestMapping("add")
    public String addUser(User user, String role_id) {
        user.setRole_id(Integer.parseInt(role_id));
        int i = userSerivcel.addUser(user);
        return "redirect:login";
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("addhtml")
    public String addhtml() {
        return "add-user";
    }

    /**
     * 跳转登录
     *
     * @return
     */
    @RequestMapping("login")
    public String login() {
        return "login";
    }

    /**
     * 登录检查
     *
     * @param name
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("loginDate")
    public String loginDate(String name, String password, HttpServletRequest request) {
        User user1 = (User) request.getSession().getAttribute("user");
        if (user1 != null) {
            request.setAttribute("role", roleMapper.findById(user1.getRole_id()));
            request.setAttribute("address", addressMapper.findAll());
            request.getSession().setAttribute("user", user1);
            List<Order> allById = orderMaper.findAllById(user1.getId());
            allById.forEach(order -> {
                order.setTotals(addressMapper.findById(order.getAddress_id()).getCosts());
            });
            request.setAttribute("orders",allById);
             if(allById.size()<=0){
                 List<Order> orders = orderMaper.findAllUser(user1.getId());
                 request.setAttribute("orders",orders);
             }

            return "logincheng";
        }
        User user = userSerivcel.loginIf(name, password);
        if (user == null) {
            request.setAttribute("msg", "登录失败");
            return "redirect:login";
        }
        List<Order> allById = orderMaper.findAllById(user.getId());
         allById.forEach(order -> {
             order.setTotals(addressMapper.findById(order.getAddress_id()).getCosts());
         });
        request.setAttribute("orders",allById);
        request.setAttribute("role", roleMapper.findById(user.getRole_id()));
        request.setAttribute("address", addressMapper.findAll());
        request.getSession().setAttribute("user", user);
        if(allById.size()<=0){
            List<Order> orders = orderMaper.findAllUser(user.getId());
            request.setAttribute("orders",orders);
        }
        return "logincheng";
    }

    @GetMapping("outlogin")
   public String outlogin(HttpServletRequest request){
         request.getSession().removeAttribute("user");
         return "login";
    }
}
